package com.ywl.study.sharding.study04;

import org.apache.shardingsphere.api.hint.HintManager;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class YmlDataSourceTest {
    public static void main(String[] args) throws IOException, SQLException {
       String file= YmlDataSourceTest.class.getClass().getResource("/yaml/hint/hint.yaml").getFile();

        HintManager hintManager=HintManager.getInstance();
        DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(new File(file));
        Connection connection=dataSource.getConnection();

        String sql="SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
        String sql2="select * from t_order";
        PreparedStatement statement=connection.prepareStatement(sql);
        //value 随便写，只要有这两个add方法，那么对应的自定义hint策略就会生效
        hintManager.addDatabaseShardingValue("t_order","");
        hintManager.addTableShardingValue("t_order",4);
        statement.setInt(1,1003);
        statement.setInt(2,4);
        ResultSet resultSet=statement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getInt(1));
            System.out.println(resultSet.getString(2));
        }
    }
}
